<?php

namespace app\app\controller;

use app\app\model\ProjectModel;
use app\app\model\ProjectWZModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class AMaterialsSupplied extends BaseCurd
{
    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = ProjectWZModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = [];

        $wz_detail = input('wz_detail', '');
        if (!empty($wz_detail)) $where[] = ['wz_detail', 'like', '%' . $wz_detail . '%'];

        // 搜索
        $project_id = input('project_id', '');
        if (!empty($project_id)) $where[] = ['project_id', 'like', '%' . $project_id . '%'];

        $page_size = input('pageSize', '20');

        $data = $this->model::where($where)->paginate($page_size);

        empty($data) && $this->error('查询为空');

        $this->success('success', $data->toArray());
    }

    /**
     * detail
     * @param $id
     * @return void
     */
    public function detail($id = 0)
    {
        !$id && $this->error('请传入id');

        $data = $this->model::where('id', $id)->find();
        empty($data) && $this->error('查询为空');

        $this->success('success', $data);
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }

    /**
     * 查询项目列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listGroup()
    {
        $name = input('name/s', '');

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['b.tc_time', '>=', $start_time];
        $where[] = ['b.kg_time', '<=', $end_time];
        if ($name) {
            $where[] = ['b.project_name|c.project_name', 'like', '%' . $name . '%'];
        }
        $data = $this->model::alias('a')
            ->leftJoin('t_project b', 'a.project_id = b.id')
            ->leftJoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)
            ->group('b.id')
            ->field('b.id,b.link_project_id,b.is_parent')
            ->select();

        $project_id_array = array_column($data->toArray(), null, 'id');
        $project_id_array = array_keys($project_id_array);

        $result = [];
        foreach ($data as $v) {
            if ($v['is_parent'] == 0) {
                $result[$v['id']] = $v;
                continue;
            }

            $result[$v['link_project_id']]['children'][$v['id']]['children'][] = $v;
        }

        $project_id_array = array_merge($project_id_array, array_keys($result));

        $project = ProjectModel::where('id', 'in', $project_id_array)
            ->field('id, project_name')
            ->select();
        $project = array_column($project->toArray(), null, 'id');

        $result = $this->getProject($result, $project);

        $this->success('success', $result);
    }

    /**
     * 查询指定项目id下的列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function ChildrenList()
    {
        $id = input('id/s');
        if (!$id) $this->error('请传入id');
        $page_size = input('pageSize', '20');
        $data = $this->model::where('project_id', $id)
            ->paginate($page_size);
        if (empty($data)) $this->error('未找到符合条件的资源');

        $this->success('success', $data);
    }
}